package net.wanglu.www.zzz.rule.artcl;

import java.util.regex.Pattern;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class Measurement extends _ArticleRule {

	public final Pattern Measurement = Pattern.compile("\\binch\\b|\\bfoot\\b|\\byard\\b|\\bmile\\b|\\bacre\\b|\\bounce\\b|\\boz\\b|\\bgill\\b|\\bpint\\b|\\bquart\\b|\\bgallon\\b|\\bpeck\\b|\\bbushel\\b|\\bgrain\\b|\\bdram\\b|\\bpound\\b|\\bstone\\b|\\bhundredweight\\b|\\bton\\b|\\bnanometre\\b|\\bmillimetre\\b|\\bcentimetre\\b|\\bdecimetre\\b|\\bmetre\\b|\\bkilometre\\b|\\bhectare\\b|\\bmillilitre\\b|\\bcentilitre\\b|\\bdecilitre\\b|\\blitre\\b|\\bdecalitre\\b|\\bhectolitre\\b|\\bkilolitre\\b|\\bmilligram\\b|\\bcentigram\\b|\\bdecigram\\b|\\bgram\\b|\\bdecagram\\b|\\bhectogram\\b|\\bkilogram\\b|\\btonne\\b|\\bnanosecond\\b|\\bmicrosecond\\b|\\bmillisecond\\b|\\bsecond\\b|\\bminute\\b|\\bhour\\b|\\bday\\b|\\bweek\\b|\\bmonth\\b|\\byear\\b|\\bdecade\\b|\\bcentury\\b|\\bquarter\\b|\\bpound\\b|\\bpenny\\b|\\bdollar\\b|\\bcent\\b|\\byuan\\b");
	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if ( NP.depth() != 2) return Rule.Inapplicable;//如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if ( "NN".equals(headword.tag())) {
			return Rule.Applicable;//如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();

		/*
		 * 名词短语中的headword
		 */
		TaggedWord headword = npt.getHeadword();
		if ( !this.Measurement.matcher(headword.word()).find()) return Rule.Inapplicable;
		
		TregexPattern pattern = null;
		TregexMatcher matcher = null;

		/*
		 * 名词短语中含有的冠词，也可能没有
		 */
		Tree dt = null;
		sb.clear();
		sb.append("/\\bDT\\b/");
		pattern = TregexPattern.compile(sb.toString());
		matcher = pattern.matcher(NP);
		if ( matcher.find()) {
			dt = matcher.getMatch();
		}

		if ( dt == null) {
			BadEgg egg = new BadEgg();
			egg.whereIsWrong = NP;
			egg.setProperty(BadEgg.Error);
			egg.setViolatedRule(this);
			return egg;									
		} else {
			String dtWord = dt.taggedYield().get(0).word();
			if( "the".equalsIgnoreCase(dtWord)) {
				BadEgg egg = new BadEgg();
				egg.whereIsWrong = NP;
				egg.setProperty(BadEgg.Error);
				egg.setViolatedRule(this);
				return egg;										
			} else if ( "a".equalsIgnoreCase(dtWord) || "an".equalsIgnoreCase(dtWord)) {
				return null;
			} else {
				return Rule.Inapplicable;
			}
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		sb.append("<p>");
		sb.append("如果是表示一这个数量，可以使用不定冠词。尤其是在表示重量、长度、时间等的词组中。");
		sb.append("</p>");			
		return sb.toString();
	}
}
